﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.Course;

public partial class sysadmin_CourseManager_Catalog : PowerCreator.LMS.Web.Base.BasePage
{
    Catalog opCatalog = new Catalog();
    CatalogCourse opCatalogCourse = new CatalogCourse();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            BinderRPT();
        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }


    }
    private bool CheckPopedom()
    {
        bool rt = false;
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup == 1 || login.VOAdvancedUser.AUserGroup == 2)
                {
                    rt = true;
                }
            }
        }

        return rt;

    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_Catalog_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    private void BinderRPT()
    {
        string search = "";
        //如果是超级管理员登录可以查看所有课程
        if (GetLoginEntity.UserName.ToLower() != "admin" && GetLoginEntity.UserName.ToLower() != "administrator")
        {
            search = String.Format("(CatalogID in (Select FkOID from TauthorityUser as a , TauthorityList as b where a. FkAuthorityID = b.FkAuthorityID and FkUserID ={0} and b.authorityType=3) or CreatorId={0})", GetLoginEntity.UserID);

        }
        DataTable dt = opCatalog.QueryCatalog(search);
        ArrayList list = opCatalog.GetCatalogTree(dt);
        if (list.Count == 0)
        {
            ListItem li = new ListItem();
            li.Text = GetLanageValueByKey("Catalog_db_d1", PowerCreator.LMS.Language.LanagetItemType.Page);
            li.Value = "0";
            list.Add(li);
        }
        rptCatalog.DataSource = list;
        rptCatalog.DataBind();
        for (int i = 0; i < rptCatalog.Items.Count; i++)
        {
            Button btnadd = (Button)rptCatalog.Items[i].FindControl("btn_Add");
            btnadd.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
            Button btn_Editor = (Button)rptCatalog.Items[i].FindControl("btn_Editor");
            btn_Editor.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o1", PowerCreator.LMS.Language.LanagetItemType.Page);
            Button btn_del = (Button)rptCatalog.Items[i].FindControl("btn_del");
            btn_del.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o2", PowerCreator.LMS.Language.LanagetItemType.Page);


            if (CheckPopedom())
            {
                btnadd.Enabled = true;
                btn_Editor.Enabled = true;
                btn_del.Enabled = true;
            }
            else
            {
                btnadd.Enabled = false;
                btn_Editor.Enabled = false;
                btn_del.Enabled = false;
            }

        }
    }
    protected void rptCatalog_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "btnadd")
        {
            string url = "AddCatalog.aspx?CatalogID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "btnedit")
        {
            string url = "EditCatalog.aspx?CatalogID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "btndel")
        {
            //if (IsLicensed("DelCatalog"))
            //{
                int count = opCatalog.CountParent(int.Parse(e.CommandArgument.ToString()));
                if (count > 1)
                {
                    //Label lblmsg = (Label)e.Item.FindControl("lbl_msg_description");
                    //lbl_msg_description.Text = "该节点下有子节点，不允许删除该节点!";
                    lbl_msg_description.Text = GetLanageValueByKey("Catalog_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                    return;
                }
                //该课程下是否还有课程
                string search = " FKCatalogcouCatalogID = " + e.CommandArgument.ToString();

                if (opCatalogCourse.QueryCatalogCourseCount(search) != 0)
                {
                   // Label lblmsg = (Label)e.Item.FindControl("lbl_msg_description");
                   // lbl_msg_description.Text = "该专业下有课程，不允许删除该专业!";
                    lbl_msg_description.Text = GetLanageValueByKey("Catalog_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                    return;
                }
                if (opCatalog.DelCatalog(int.Parse(e.CommandArgument.ToString())))
                {
                    BinderRPT();
                }
            //}
            //else
            //{
            //  // Label lblmsg = (Label)e.Item.FindControl("lbl_msg_description");
            //    //lbl_msg_description.Text = "您没有删除专业的权限!";
            //    lbl_msg_description.Text = GetLanageValueByKey("Catalog_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
            //    Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            //}
        }
    }
}
